package cn.stylefeng.guns.modular.banner.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.banner.entity.Banner;
import cn.stylefeng.guns.modular.banner.enums.BannerExceptionEnum;
import cn.stylefeng.guns.modular.banner.mapper.BannerMapper;
import cn.stylefeng.guns.modular.banner.pojo.request.BannerRequest;
import cn.stylefeng.guns.modular.banner.service.BannerService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * banner业务实现层
 *
 * @author wxz
 * @date 2022/01/11 11:53
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

	@Override
    public void add(BannerRequest bannerRequest) {
        Banner banner = new Banner();
        BeanUtil.copyProperties(bannerRequest, banner);
        this.save(banner);
    }

    @Override
    public void del(BannerRequest bannerRequest) {
        Banner banner = this.queryBanner(bannerRequest);
        this.removeById(banner.getBannerId());
    }

    @Override
    public void edit(BannerRequest bannerRequest) {
        Banner banner = this.queryBanner(bannerRequest);
        BeanUtil.copyProperties(bannerRequest, banner);
        this.updateById(banner);
    }

    @Override
    public Banner detail(BannerRequest bannerRequest) {
        return this.queryBanner(bannerRequest);
    }

    @Override
    public PageResult<Banner> findPage(BannerRequest bannerRequest) {
        LambdaQueryWrapper<Banner> wrapper = createWrapper(bannerRequest);
        Page<Banner> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<Banner> findList(BannerRequest bannerRequest) {
        LambdaQueryWrapper<Banner> wrapper = this.createWrapper(bannerRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author wxz
     * @date 2022/01/11 11:53
     */
    private Banner queryBanner(BannerRequest bannerRequest) {
        Banner banner = this.getById(bannerRequest.getBannerId());
        if (ObjectUtil.isEmpty(banner)) {
            throw new ServiceException(BannerExceptionEnum.BANNER_NOT_EXISTED);
        }
        return banner;
    }

    /**
     * 创建查询wrapper
     *
     * @author wxz
     * @date 2022/01/11 11:53
     */
    private LambdaQueryWrapper<Banner> createWrapper(BannerRequest bannerRequest) {
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();

        Integer bannerId = bannerRequest.getBannerId();
        Long fileId = bannerRequest.getFileId();
        String bannerName = bannerRequest.getBannerName();
        String link = bannerRequest.getLink();
        Integer sort = bannerRequest.getSort();
        Integer status = bannerRequest.getStatus();

        queryWrapper.eq(ObjectUtil.isNotNull(bannerId), Banner::getBannerId, bannerId);
        queryWrapper.eq(ObjectUtil.isNotNull(fileId), Banner::getFileId, fileId);
        queryWrapper.like(ObjectUtil.isNotEmpty(bannerName), Banner::getBannerName, bannerName);
        queryWrapper.like(ObjectUtil.isNotEmpty(link), Banner::getLink, link);
        queryWrapper.eq(ObjectUtil.isNotNull(sort), Banner::getSort, sort);
        queryWrapper.eq(ObjectUtil.isNotNull(status), Banner::getStatus, status);

        return queryWrapper;
    }

}